<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{active}">
      <span class="placeholder" v-if="!seled.fullSels">请选择配送地址</span>
      <span class="value" v-else>{{seled.fullSels}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" @click="chooseCity(city)" v-for="city in cityList" :key="city.code">{{city.name}}</span>
    </div>
  </div>
</template>
<script>
import { onMounted, ref, reactive } from 'vue'
import axios from 'axios'
// 获取城市列表
const useCity = () => {
  const cityList = ref([])
  const cityListBackup = []
  const getCity = async () => {
    const res = await axios({
      method: 'get',
      url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    })
    // console.log(res.data)
    cityList.value = res.data
    // 不改变地址
    cityListBackup.push(...res.data)
  }

  onMounted(() => {
    getCity()
  })

  return { cityList, cityListBackup }
}
export default {
  name: 'XtxCity',
  emits: ['change'],
  setup (props, { emit }) {
    const active = ref(false)
    // 点击选中城市数据
    const seled = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择
      fullSels: ''
    })
    const open = () => {
      active.value = true
      // 重新选择
      cityList.value = cityListBackup
    }
    const close = () => {
      active.value = false
    }
    const toggleDialog = () => {
      if (active.value) {
        close()
      } else {
        open()
      }
    }

    // 获取城市列表
    const { cityList, cityListBackup } = useCity()

    // 选择城市
    const chooseCity = (city) => {
      cityList.value = city.areaList
      if (city.level === 0) {
        seled.provinceCode = city.code
        seled.provinceName = city.name
      } else if (city.level === 1) {
        seled.cityCode = city.code
        seled.cityName = city.name
      } else {
        seled.countryCode = city.code
        seled.countryName = city.name

        // 拼接最终地址
        seled.fullSels = `${seled.provinceName} ${seled.cityName} ${seled.countryName}`
        // 关闭选项
        close()
        // 传递给父组件
        emit('change', seled)
      }
    }
    return { active, toggleDialog, cityList, chooseCity, seled }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
